package fi.profecons.csswindows.toolkit;

import java.io.File;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FilenameUtils;

import fi.profecons.csswindows.ActionListener;
import fi.profecons.csswindows.Container;
import fi.profecons.csswindows.Element;

public class FileUpload extends Container implements ActionListener {

	private String relativePath;

	private String path;

	private int percent;

	private Form uploadForm;

	private Input fileInput;

	private Button submitButton;

	private StatusBar statusBar;

	private IFrame iFrame;

	private String fileName;

	private File file;

	private boolean uploadOk = false;

	public FileUpload(int x, int y, String relativePath) {
		super();
		this.statusBar = new StatusBar();
		this.statusBar.setBounds(10, 10, 300, 20);
		this.statusBar.hide();
		this.relativePath = relativePath;
		addCommand("new FileUpload('" + getId() + "');");
		add(statusBar);
		this.iFrame = createUploadFrame();
		add(iFrame);
		this.setLocation(x, y);
		this.setSize(320, 40);

	}

	private IFrame createUploadFrame() {
		IFrame iFrame = new IFrame();
		iFrame.setBounds(0, 0, 340, 60);
		this.uploadForm = new Form("", "multipart/form-data", "POST");
		this.uploadForm.setBounds(10, 10, 300, 40);
		this.fileInput = new Input("file", "file");
		this.submitButton = new Button("OK", "lisaa");
		this.submitButton.setActionListener(this);
		this.submitButton.setBounds(210, 0, 80, 20);
		this.fileInput.setBounds(0, 0, 200, 20);
		this.fileInput.setActionListener(this);
		this.uploadForm.setAction("?action=FILE_UPLOAD");
		this.uploadForm.add(this.fileInput);
		this.uploadForm.add(this.submitButton);
		iFrame
				.setSource("?action=FILE_FETCH_TEXT&filename=upload.html&path=/html/");
		iFrame.add(this.uploadForm);
		return iFrame;
	}

	public void initialize() {
	}

	public void processFileItem(FileItem item) {
		fileName = FilenameUtils.getName(item.getName());
		this.file = new File(path, fileName);
		try {
			item.write(this.file);
			uploadOk = true;

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void uploadReady() {

	}

	public void uploadFailure() {
	}

	public void updateUploadStatus(int percent) {
		this.percent = percent;

	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getRelativePath() {
		return relativePath;
	}

	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}

	public int getPercent() {
		return percent;
	}

	public void setPercent(int percent) {
		this.percent = percent;
	}

	public void handleAction(Element e, String action) {
		if (action.equals("change") || action.equals("lisaa")) {
			this.iFrame.hide();
			this.statusBar.show();
			this.statusBar.start();
			this.submit();
		}
	}

	public void reset() {
		remove(this.iFrame);
		this.iFrame = createUploadFrame();
		add(this.iFrame);
		this.statusBar.hide();
		this.statusBar.setValue(0);
	}

	private void submit() {
		setFileUpload(this);
		addCommand("getElement('" + this.iFrame.getId()
				+ "').document.getElementById('" + uploadForm.getId()
				+ "').submit();");
	}

	public StatusBar getStatusBar() {
		return statusBar;
	}

	public IFrame getIFrame() {
		return iFrame;
	}

	public String getFileName() {
		return fileName;
	}

	public boolean isUploadOk() {
		return uploadOk;
	}

	public File getFile() {
		return file;
	}

	public void handleAction(Element target, Element source, String action) {
	}

}
